package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.History;
import com.ruoyi.system.mapper.HistoryMapper;
import com.ruoyi.system.service.IHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 浏览历史Controller
 *
 * @author AousVolad
 * @date 2024-06-27
 */
@RestController
@RequestMapping("/history/history")
public class HistoryController extends BaseController {
    @Autowired
    private IHistoryService historyService;

    @Autowired
    private HistoryMapper historyMapper;

    /**
     * 查询浏览历史列表
     */
    @PreAuthorize("@ss.hasPermi('history:history:list')")
    @GetMapping("/list")
    public TableDataInfo list(History history) {
        startPage();
        List<History> list = historyService.selectHistoryList(history);
        return getDataTable(list);
    }

    /**
     * 导出浏览历史列表
     */
    @PreAuthorize("@ss.hasPermi('history:history:export')")
    @Log(title = "浏览历史", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, History history) {
        List<History> list = historyService.selectHistoryList(history);
        ExcelUtil<History> util = new ExcelUtil<History>(History.class);
        util.exportExcel(response, list, "浏览历史数据");
    }

    /**
     * 获取浏览历史详细信息
     */
    @PreAuthorize("@ss.hasPermi('history:history:query')")
    @GetMapping(value = "/{historyId}")
    public AjaxResult getInfo(@PathVariable("historyId") Long historyId) {
        return success(historyService.selectHistoryByHistoryId(historyId));
    }

    /**
     * 新增浏览历史
     */
    @PreAuthorize("@ss.hasPermi('history:history:add')")
    @Log(title = "浏览历史", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody History history) {
        return toAjax(historyService.insertHistory(history));
    }

    /**
     * 修改浏览历史
     */
    @PreAuthorize("@ss.hasPermi('history:history:edit')")
    @Log(title = "浏览历史", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody History history) {
        return toAjax(historyService.updateHistory(history));
    }

    /**
     * 删除浏览历史
     */
    @PreAuthorize("@ss.hasPermi('history:history:remove')")
    @Log(title = "浏览历史", businessType = BusinessType.DELETE)
    @DeleteMapping("/{historyIds}")
    public AjaxResult remove(@PathVariable Long[] historyIds) {
        return toAjax(historyService.deleteHistoryByHistoryIds(historyIds));
    }


    @PreAuthorize("@ss.hasPermi('history:history:remove')")
    @Log(title = "浏览历史", businessType = BusinessType.DELETE)
    @DeleteMapping
    public AjaxResult removeAll(String userId) {
        return toAjax(historyMapper.clearAll(userId));
    }
}
